﻿@{
    ViewBag.Title = "FetchVideoByAjax";
}

<h2>获取视频</h2>
<div class="am-g">
    <div class="am-u-sm-4">
        <a class="am-btn am-btn-warning" onclick="fetchVideo(this)">获取Video</a>
    </div>
    <div class="am-u-sm-4">
        <a class="am-btn am-btn-success" onclick="fetchGif(this)">获取Gif</a>
    </div>
    <div class="am-u-sm-4">
        <a class="am-btn am-btn-danger" onclick="fetchTxt(this)">测试</a>
    </div>
</div>
@section Scripts{
    <script>
        $(function () {

        })

        function fetchVideo(obj) {
            $(obj).attr("disabled", "disabled");
            for (var i = 1; i <= 15; i++) {
                //此处需使用闭包
                (function (i) {
                    setTimeout(function () {                        
                        getData(i);
                    }, 1000 * i);
                })(i);

            }
        }

        //function fetchVideo(obj) {
        //    $(obj).attr("disabled", "disabled");            
        //    getData(1);
        //}



        var fetchNum = 0;
        function getData(pagenum) {
            console.log('start');
            $.ajax({
                type: "GET",
                url: 'http://c.v.qq.com/vchannelinfo',
                dataType: 'jsonp',
                jsonp: 'callback',
                jsonpCallback: 'jQuery1910526401350432103_1494166788794',
                data:{
                    otype:'json',
                    uin:'c6ab2450686fde0adef1e7c7ee68744c',
                    qm:1,
                    pagenum: pagenum,
                    num:24,
                    sorttype:0,
                    orderflag:0,  
                    low_login:1,
                    _:1494166788803
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {  //#3这个error函数调试时非常有用，如果解析不正确，将会弹出错误框
                    alert(XMLHttpRequest.status);
                    alert(XMLHttpRequest.readyState);
                    alert(textStatus); // paser error;
                },
                success: function (data) {
                    console.log(data);
                    if (data.videolst && data.videolst.length>0) {
                        $.each(data.videolst, function (index, item) {

                            saveVideo('xuduba', item.vid, item.url, item.title, item.desc, item.pic);
                        })
                    }
                },
                beforeSend: function () {
                },
                complete: function () {
                }
            });
        }


        function saveVideo(type,vid,url,title, desc, pic) {
            $.ajax({
                type: "POST",
                url: '@Url.Action("SaveVideo", "AutoFetch")',
                dataType: "json",
                data: {
                    type: type,
                    vid: vid,
                    url:url,
                    title: title,
                    desc: desc,
                    pic: pic
                },
                success: function (data) {
                    //console.log('writeLog', data);
                }
            });
        }
    </script>
}


